//
//  SignupViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/18.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import ObjectMapper

class SignupViewController: BaseViewController {
    @IBOutlet weak var topImageView: UIImageView!
    
    @IBOutlet weak var idTF: UITextField!
    
    @IBOutlet weak var sheBaoTF: UITextField!
    
    @IBOutlet weak var nameTF: UITextField!
    
    @IBOutlet weak var passwordTF: UITextField!
    
    @IBOutlet weak var phoneTF: UITextField!
    
    @IBOutlet weak var signupButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "注册"
        // Do any additional setup after loading the view.
        
        idTF.delegate = self
        sheBaoTF.delegate = self
//        sheBaoTF.keyboardType = .numberPad
        
        nameTF.delegate = self
        passwordTF.delegate = self
        phoneTF.delegate = self
        phoneTF.keyboardType = .numberPad
        
        signupButton.addTarget(self, action: #selector(signupButtonAction), for: .touchUpInside)
        
//        #if DEBUG
//        idTF.text = "411082198110164857"
//        sheBaoTF.text = "JSE115970293"
//        nameTF.text = "周玉平"
//        passwordTF.text = "868119"
//        phoneTF.text = "18221118968"
//        #endif
    }

    @objc func signupButtonAction() {
        guard let idString = idTF.text, idString.count > 0 else {
            CDUtility.showMSG(message: idTF.placeholder ?? "")
            return
        }
        
        if !CDUtility.checkUserIdCard(idString) {
            CDUtility.showMSG(message: "请输入正确的身份证号码~")
            return
        }
        
        guard let sheBaoString = sheBaoTF.text, sheBaoString.count > 0 else {
            CDUtility.showMSG(message: sheBaoTF.placeholder ?? "")
            return
        }
        
        guard let nameString = nameTF.text, nameString.count > 0 else {
            CDUtility.showMSG(message: nameTF.placeholder ?? "")
            return
        }
        
        var pwdString = passwordTF.text ?? ""
        if pwdString.count == 0 && pwdString.count >= 6{
            pwdString = String(idString.suffix(6))
            passwordTF.text = pwdString
        }
        
        if pwdString.count < 6 {
            CDUtility.showMSG(message: "密码至少6位")
            return
        }
        
        guard let phoneString = phoneTF.text, phoneString.count > 0 else {
            CDUtility.showMSG(message: phoneTF.placeholder ?? "")
            return
        }
        
        let predicate = NSPredicate(format: "SELF MATCHES %@", "^1[0-9]{10}$")
        if !predicate.evaluate(with: phoneString){
            makeToast("请输入正确的手机号")
            return
        }
        
//        {"IDNum":"320522196901156190",
//            "Name":"杨培良",
//            "Password":"123456",
//            "PerCardNum":"JSE000001079",
//            "TelNum":"13771911349"}

        
        let values = ["IDNum": idString,
                      "Name": nameString,
                      "Password": pwdString,
                      "PerCardNum": sheBaoString,
                      "TelNum": phoneString].convertToJsonString()
        let p0 = "<CodeName>"+"userregister"+"</CodeName>"
        let p1 = "<json>"+values+"</json>"
        
        let paramString = p0 + p1
        
        NetworkingHandle.oldfetchNetworkData(method: "ExeHNService", paramString: paramString, success: { (dict) in
            if let code = dict["Code"] as? Int,code == 0 {
                UserDefaults.standard.set(idString, forKey: CDUserDefaultKey.loginID)
                UserDefaults.standard.set(pwdString, forKey: CDUserDefaultKey.loginPassword)
                UserDefaults.standard.synchronize()
                CDUtility.showMSG(message: "注册成功，请登录~")
                self.navigationController?.popViewController(animated: true)
                return
            }
            if let code = dict["Code"] as? Int,code == 1 {
                if let msgString = dict["Message"] as? String {
                    //                    let data = msgString.convertToDictionary()
                    CDUtility.showMSG(message: msgString)
                }
            }
            
        }) {
            print("")
        }
    }
}

extension SignupViewController: UITextFieldDelegate {
    
    
}
